/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.common.other;

import com.googlecode.jaden.common.enums.LayerType;
import static com.googlecode.jaden.ui.Constants.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Utils {

    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("jaden");

    public static String translate(String key, String... parameters) {
        String value = BUNDLE.getString(key);
        int i = 0;
        for (String parameter : parameters) {
            value = value.replace("{" + i++ + '}', parameter);
        }
        return value;
    }

//    public static String translate(LayerType layerType) {
//        switch (layerType) {
//            case Normal:
//                return translate(LAYER_TYPE_NORMAL);
//            case Soft:
//                return translate(LAYER_TYPE_SOFT);
//            case Sharp:
//                return translate(LAYER_TYPE_SHARP);
//        }
//        throw new IllegalStateException("Wot layer type? " + layerType);
//    }
//
    public static KeyStroke getKeyStroke(String maskKey) {
        String key;
        String mask;
        if (maskKey.contains("-")) {
            String[] strings = maskKey.split("-");
            mask = strings[0];
            key = strings[1];
        } else {
            mask = "";
            key = maskKey;
        }
        try {
            Field[] fields = KeyEvent.class.getFields();
            int keyChar = 0;
            for (Field field : fields) {
                if (field.getName().equals("VK_" + key)) {
                    keyChar = field.getInt(field);
                }
            }
            if (keyChar == 0) {
                throw new IllegalStateException("Bad key " + key);
            }
            if (mask.equalsIgnoreCase("ctrl")) {
                return KeyStroke.getKeyStroke(keyChar, Event.CTRL_MASK);
            } else if (mask.length() == 0) {
                return KeyStroke.getKeyStroke(keyChar, 0);
            } else {
                throw new IllegalStateException("Bad mask " + mask);
            }
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Reflection error", e);
        }
    }

    private static final Logger logger = Logger.getLogger(Utils.class.getName());

    public static boolean isBlankOrNull(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static void handleException(Exception e) {
        logger.log(Level.SEVERE, "Jaden", e);
    }

    public static String underline(String str, boolean strong) {
        if (str == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            sb.append(strong ? '=' : '-');
        }
        return sb.toString();
    }


    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    public static String replace(String value, String replace, String with) {
        return value.replace(replace.charAt(0), with.charAt(0));
    }

    public static String removeChar(String value, String remove) {

        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
            char c = value.charAt(i);
            if (!(c == remove.charAt(0))) {
                buf.append(c);
            }
        }
        return buf.toString();
    }

    public static String topAndTail(String stringLimiter, String stringValue) {
        if (stringValue == null) {
            return null;
        }
        if (stringValue.startsWith(stringLimiter) && stringValue.endsWith(stringLimiter)) {
            return stringValue.substring(1, stringValue.length() - 1);
        }
        return stringValue;
    }

    public static String numericToInternal(String external, String decimal, String thousands) {
        while (external.contains(thousands)) {
            int pos = external.indexOf(thousands);
            external = external.substring(0, pos) + external.substring(pos + 1);
        }
        int pos = external.indexOf(decimal);
        if (pos >= 0) {
            String left = external.substring(0, pos);
            String right = external.substring(pos + 1);
            return left + '.' + right;
        }
        return external;
    }

    public static String numericToExternal(String internal, String decimal) {
        int pos = internal.indexOf('.');
        if (pos >= 0) {
            String left = internal.substring(0, pos);
            String right = internal.substring(pos + 1);
            return left + decimal + right;
        }
        return internal;
    }
}
